package cn.tedu.store.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.tedu.store.bean.Address;
import cn.tedu.store.bean.CartVo;
import cn.tedu.store.bean.OrderItemVo;
import cn.tedu.store.service.IAddressService;
import cn.tedu.store.service.ICartService;
import cn.tedu.store.service.IOrderService;

@Controller
@RequestMapping("/order")
public class OrderController extends BaseController{
	
	@Resource
	private ICartService cartService;
	@Resource
	private IAddressService addressService;
	
	@Resource
	private IOrderService orderService;
	@RequestMapping("/showOrder.do")
	public String showOrder(HttpSession session,Integer[] ids,Map<String,Object> map){
		//获取商品信息的集合
		List<CartVo> listCartVo=cartService.getOrderByUserid(this.getUid(session), ids);
		//获取地址信息的集合
		List<Address> listAddress=addressService.getAllAddressByUid(this.getUid(session));
		map.put("listCartVo", listCartVo);
		map.put("listAddress",listAddress);
		session.setAttribute("listCartVo", listCartVo);
		return "orderConfirm";
	}
	
	@RequestMapping("/payment.do")
	public String payment(HttpSession session){
		Integer uid=this.getUid(session);
		List<CartVo> listCartVo=(List<CartVo>) session.getAttribute("listCartVo");
		Integer orderid=orderService.addOrder(uid, listCartVo);
		session.setAttribute("orderid", orderid);
		return "payment";
	}
	
	@RequestMapping("/pay.do")
	public String pay(HttpSession session){
		Integer orderid=(Integer) session.getAttribute("orderid");
		orderService.updateStatus(orderid);
		//移除session中的orderid和listCartVo
		session.removeAttribute("orderid");
		session.removeAttribute("listCartVo");
		return "pay_success";
	}
	
	@RequestMapping("/showOrderItem.do")
	public String showOrderItem(HttpSession session,Map<String,List<OrderItemVo>> map){
		List<OrderItemVo> listOrderItemVo=orderService.getOrderItems(this.getUid(session));
		map.put("listOrderItemVo",listOrderItemVo);
		return "orders";
	}
	
}
